package com.unionftech.common.permissions.tip.impl.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.unionftech.common.R;
import com.unionftech.common.common.language.DynamicResourceManager;
import com.unionftech.common.permissions.tip.bean.PermissionTipInfo;

import java.util.List;

public class PermissionSceneDialog extends DialogFragment {
    private final Context mContext;
    private final List<PermissionTipInfo> mList;
    private OnDismissListener mDismissListener;

    public PermissionSceneDialog(Context context, List<PermissionTipInfo> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public void onStart() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = WindowManager.LayoutParams.MATCH_PARENT;
                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(params);
            }
        }
        super.onStart();
    }

    public void setDismissListener(OnDismissListener dismissListener) {
        this.mDismissListener = dismissListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.setGravity(Gravity.CENTER);
                window.getDecorView().setPadding(0, 0, 0, 0);
            }
        }
        return LayoutInflater.from(mContext).inflate(R.layout.layout_permission_scene_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView tvPermissionTitle = (TextView) view.findViewById(R.id.tv_permission_title);
        TextView tvPermissionScene = (TextView) view.findViewById(R.id.tv_permission_scene_tip);
        PermissionTipInfo tipInfo = mList.get(0);
        tvPermissionTitle.setText(tipInfo.getTitle());
        tvPermissionScene.setText(tipInfo.getTip());
        TextView btnConfirm = view.findViewById(R.id.btn_permission_confirm);
        btnConfirm.setText(DynamicResourceManager.getString(R.string.confirm));
        btnConfirm.setOnClickListener(v -> {
            dismiss();
            mDismissListener.onAction();
        });
    }

}
